package com.example.testrecorder.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testrecorder.EditIssueActivity;
import com.example.testrecorder.IssueDetailActivity;
import com.example.testrecorder.MyApplication;
import com.example.testrecorder.R;
import com.example.testrecorder.domain.Issue;
import com.example.testrecorder.domain.IssueShowed;
import com.example.testrecorder.domain.SelectItem;
import com.example.testrecorder.utils.DateUtil;
import com.example.testrecorder.utils.MyDialog;
import com.example.testrecorder.utils.SelectDialog;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/5/26 20:31
 * @version 1.0
 */
public class IssueShowedAdapter extends RecyclerView.Adapter<IssueShowedAdapter.ViewHolder> {
    private List<IssueShowed> list;
    private Context context;
    private SelectDialog selectDialog;

    public IssueShowedAdapter(List<IssueShowed> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.issue_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        IssueShowed issueShowed = list.get(position);
        int isId = issueShowed.getId();
        String solved = issueShowed.getSolved();//问题解决了没有
        String severity = issueShowed.getSeverity();//严重程度
        String title = issueShowed.getTitle();
        String content = issueShowed.getContent();
        String proposeTime = issueShowed.getProposeTime();
        if(IssueShowed.FINISHED.equals(solved)){//已解决
            holder.leftIv.setImageResource(R.drawable.finished);
        }else if(IssueShowed.UNFINISHED.equals(solved)){//未解决
            holder.leftIv.setImageResource(R.drawable.unfinished);
        }else {//无需解决
            holder.leftIv.setImageResource(R.drawable.need_not_finished);
        }
        /*if(IssueShowed.HEIGH.equals(severity)){//高
            holder.rightIv.setImageResource(R.drawable.high);
        }else if(IssueShowed.MIDDLE.equals(severity)){//中
            holder.rightIv.setImageResource(R.drawable.middle);
        }else {
            holder.rightIv.setImageResource(R.drawable.low);//低
        }*/
        holder.title.setText(title.length()>=10?title.substring(0,9)+"...":title);
        holder.content.setText(content.length()>=40?content.substring(0,39)+"...":content);//如果内容长度大于等于40个字符，就保留39个字符再加上三个点...
        holder.time.setText(proposeTime);
        holder.idTv.setText(""+isId);
        holder.cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, IssueDetailActivity.class);
                intent.putExtra("id",isId);
                context.startActivity(intent);
            }
        });
        holder.cv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ArrayList<SelectItem> l = new ArrayList<>();
                l.add(new SelectItem(R.drawable.ic_visibility,"查看"));
                l.add(new SelectItem(R.drawable.ic_edit,"编辑"));
                l.add(new SelectItem(R.drawable.ic_delete,"删除"));
                selectDialog=new SelectDialog(context, l, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if(selectDialog !=null){
                            selectDialog.dismiss();
                        }
                        switch (position){
                            case 0:
                                Intent intent=new Intent(context,IssueDetailActivity.class);
                                intent.putExtra("id",isId);
                                context.startActivity(intent);
                                break;
                            case 1:
                                Intent intent2=new Intent(context, EditIssueActivity.class);
                                intent2.putExtra("id",isId);
                                context.startActivity(intent2);
                                break;
                            case 2:
                                MyDialog myDialog=new MyDialog(context);
                                myDialog.setTitle("警告");
                                myDialog.setTitleTextColor("#F17271");
                                myDialog.setContent("您确定要删除标题为“"+title+"”的记录吗？");
                                myDialog.show();
                                myDialog.setOnNegativeListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        myDialog.dismiss();
                                    }
                                });
                                myDialog.setOnPositiveListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        int delete = LitePal.delete(Issue.class, isId);
                                        if(delete>0){
                                            Toast.makeText(context,"删除成功！",Toast.LENGTH_SHORT).show();
                                            list.clear();
                                            List<Issue> all = LitePal.where("user_id=?",""+ MyApplication.currUser.getId()).order("proposetime desc").find(Issue.class);
                                            for (int i = 0; i < all.size(); i++) {
                                                Issue issue = all.get(i);
                                                list.add(new IssueShowed(issue.getId(),issue.getTitle(),issue.getContent(), DateUtil.getFormatDateStr(issue.getProposeTime()),issue.getSolved(),issue.getSeverity()));
                                            }
                                            notifyDataSetChanged();
                                            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("reset"));//发送重置顶部筛选栏的广播
                                        }else {
                                            Toast.makeText(context,"删除失败！",Toast.LENGTH_SHORT).show();
                                        }
                                        myDialog.dismiss();
                                    }
                                });
                                break;
                            default:
                        }
                    }
                });
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        ImageView leftIv,rightIv;//左边的代表是否已完成，右边的代表严重程度
        TextView title,content,time,idTv;
        public ViewHolder(@NonNull View itemView) {//构造器
            super(itemView);
            cv=itemView.findViewById(R.id.cv);
            leftIv=itemView.findViewById(R.id.leftIv);
            rightIv=itemView.findViewById(R.id.rightIv);
            title=itemView.findViewById(R.id.titleTv);
            content=itemView.findViewById(R.id.contentTv);
            time=itemView.findViewById(R.id.timeTv);
            idTv=itemView.findViewById(R.id.idTv);
        }
    }
}
